package com.sun.weekend.view;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.sun.weekend.R;

/**
 * @author Administrator
 */

public class CustomDialog extends Dialog {

    LinearLayout linearLayout;
    View view ;
    public CustomDialog(Context context) {
        super(context,0);
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);
    }

    public void showComment(final CommentListener commentListener){
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.send_comment_dialog,null);           
            linearLayout.addView(view,new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            final EditText editText = (EditText) view.findViewById(R.id.comment);
            Button button = (Button) view.findViewById(R.id.send_comment);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commentListener.onComment(editText.getText().toString());
                }
            });
        }
        show();
    }

    @Override
    public void show() {
        super.show();
        Window mWindow = getWindow();
        WindowManager.LayoutParams layoutparams = mWindow.getAttributes();
        DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
        layoutparams.width = metrics.widthPixels;
        layoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindow.setAttributes(layoutparams);
        mWindow.setGravity(Gravity.BOTTOM);
    }

    public interface CommentListener{
        void onComment(String comment);
    }
}
